package com.itzxx.huazhun.utils.ble

@OptIn(ExperimentalUnsignedTypes::class)
object BleRequestData {
    // 获取协调器数据
    val GET_COORDINATOR_INFO_REQUEST_DATA = ubyteArrayOf(
        BleConstant.PROTOCOL_HEADER1,
        BleConstant.PROTOCOL_HEADER2,
        BleConstant.Cmd.GET_COORDINATOR_INFO_REQUEST,
        0x00u,
        0x00u,
        BleConstant.PROTOCOL_END1,
        BleConstant.PROTOCOL_END2,
    ).toByteArray()

    /**
     * 生成 获取探头数据 请求
     */
    fun generateGetProbeInfoRequestData(mac: UByteArray): ByteArray {
        val uByteMutableList = mutableListOf<UByte>()
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER1)
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER2)
        uByteMutableList.add(BleConstant.Cmd.GET_PROBE_INFO_REQUEST)
        uByteMutableList.add(0x08u)
        uByteMutableList.addAll(mac)
        uByteMutableList.add(generateCRC(uByteMutableList))
        uByteMutableList.add(BleConstant.PROTOCOL_END1)
        uByteMutableList.add(BleConstant.PROTOCOL_END2)
        return uByteMutableList.toTypedArray().toUByteArray().toByteArray()
    }

    /**
     * 生成 读取采集信息 请求
     */
    fun generateReadSamplingInfoRequestData(
        mac: UByteArray,
        readIndex: Int,
        readSize: Int = 4,
    ): ByteArray {
        val uByteMutableList = mutableListOf<UByte>()
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER1)
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER2)
        uByteMutableList.add(BleConstant.Cmd.READ_SAMPLING_INFO_REQUEST)
        uByteMutableList.add(0x0Au)
        uByteMutableList.addAll(mac)
        uByteMutableList.add(readIndex.toUByte())
        uByteMutableList.add(readSize.toUByte())
        uByteMutableList.add(generateCRC(uByteMutableList))
        uByteMutableList.add(BleConstant.PROTOCOL_END1)
        uByteMutableList.add(BleConstant.PROTOCOL_END2)
        return uByteMutableList.toTypedArray().toUByteArray().toByteArray()
    }

    /**
     * 生成 读取采集数据 请求
     */
    fun generateReadSamplingDataRequestData(
        mac: UByteArray,
        readIndex: Int,
        readNum: Int,//一次读取几笔计算： 74/(路数*2字节） = 74/(2*2) = 18
    ): ByteArray {
        val uByteMutableList = mutableListOf<UByte>()
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER1)
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER2)
        uByteMutableList.add(BleConstant.Cmd.READ_SAMPLING_DATA_REQUEST)
        uByteMutableList.add(0x10u)
        uByteMutableList.addAll(mac)
        uByteMutableList.addAll(int2uByteArray(readIndex, 4))
        uByteMutableList.addAll(int2uByteArray(readNum, 4))
        uByteMutableList.add(generateCRC(uByteMutableList))
        uByteMutableList.add(BleConstant.PROTOCOL_END1)
        uByteMutableList.add(BleConstant.PROTOCOL_END2)
        return uByteMutableList.toTypedArray().toUByteArray().toByteArray()
    }

    /**
     * 生成 设置探头参数 请求
     */
    fun generateSetProbeParameterRequestData(
        mac: UByteArray,
        startTime: Int,
        duration: Int,
        samplingInterval: Int,
        currentTime: Int,
    ): ByteArray {
        val uByteMutableList = mutableListOf<UByte>()
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER1)
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER2)
        uByteMutableList.add(BleConstant.Cmd.SET_PROBE_PARAMETER_REQUEST)
        uByteMutableList.add(0x18u)
        uByteMutableList.addAll(mac)
        uByteMutableList.addAll(long2uByteArray(startTime, 4))
        uByteMutableList.addAll(long2uByteArray(duration, 4))
        uByteMutableList.addAll(int2uByteArray(samplingInterval, 4))
        uByteMutableList.addAll(long2uByteArray(currentTime, 4))
        uByteMutableList.add(generateCRC(uByteMutableList))
        uByteMutableList.add(BleConstant.PROTOCOL_END1)
        uByteMutableList.add(BleConstant.PROTOCOL_END2)
        return uByteMutableList.toTypedArray().toUByteArray().toByteArray()
    }

    /**
     * 生成 清除采集数据 请求
     */
    fun generateClearProbeSamplingDataRequestData(mac: UByteArray): ByteArray {
        val uByteMutableList = mutableListOf<UByte>()
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER1)
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER2)
        uByteMutableList.add(BleConstant.Cmd.CLEAR_PROBE_SAMPLING_DATA_REQUEST)
        uByteMutableList.add(0x08u)
        uByteMutableList.addAll(mac)
        uByteMutableList.add(generateCRC(uByteMutableList))
        uByteMutableList.add(BleConstant.PROTOCOL_END1)
        uByteMutableList.add(BleConstant.PROTOCOL_END2)
        return uByteMutableList.toTypedArray().toUByteArray().toByteArray()
    }

    /**
     * 生成 校准采集气压 请求
     */
    fun generateAirPressureCalibrationRequestData(
        mac: UByteArray,
        model: Int,
        code: Int,
    ): ByteArray {
        val uByteMutableList = mutableListOf<UByte>()
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER1)
        uByteMutableList.add(BleConstant.PROTOCOL_HEADER2)
        uByteMutableList.add(BleConstant.Cmd.AIR_PRESSURE_CALIBRATION_REQUEST)
        uByteMutableList.add(0x0Bu)
        uByteMutableList.addAll(mac)
        uByteMutableList.add(model.toUByte())
        uByteMutableList.addAll(int2uByteArray(code, 2))
        uByteMutableList.add(generateCRC(uByteMutableList))
        uByteMutableList.add(BleConstant.PROTOCOL_END1)
        uByteMutableList.add(BleConstant.PROTOCOL_END2)
        return uByteMutableList.toTypedArray().toUByteArray().toByteArray()
    }
}